import java.util.Arrays;
import java.util.Stack;

public class Sort {
    // 直接插入排序：
    // 时间复杂度：最坏情况：O(N^2) 逆序情况
    //           最好情况：O(N)  有序情况
    //稳定性:稳定
    public static void insertSort(int[] array) {
        for(int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i -1;
            for(; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void shell(int[] array, int gap) {
        for(int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for(; j >= 0; j-=gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    //时间复杂度：O(N^1.3)
    //空间复杂度：O(1)
    //稳定性：不稳定
    public static void shellSort(int[] array) {
        int gap = array.length;
        while(gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }


    //选择排序
    //时间复杂度：O(N^2) ，不管有没有序都是这个时间复杂度
    //空间复杂度：O(1)
    //稳定性：不稳定
    public static void selectSort(int[] array) {
        for(int i = 0; i< array.length; i++) {
            int minIndex = i;
            for(int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }

    //双指针，同时找最大和最小
    public static void selectSort1(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            int minIndex = left;
            int maxIndex = left;
            for(int i = left+1; i <= right; i++) {
                if(array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if(maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //时间复杂度：O(N*logN)
    //空间复杂度：
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while(end > 0) {
            swap(array,end,0);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for(int i = (array.length-1-1)/2; i >= 0; i--) {
            shiftDown(array,i,array.length);
        }
    }

    private static void shiftDown(int[] array, int parent, int len) {
        int child = 2 * parent + 1;
        while(child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2) 对数据不敏感，有序无序都是这个复杂度
     * 加了优化后的时间复杂度最好情况可以达到 O(N)
     * 空间复杂度：O(1)
     * 稳定性：稳定 冒泡 插入
     * @param array
     */

    public static void bubbleSort(int[] array) {
        for(int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for(int j = 0; j < array.length -1 -i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if(!flag) {
                break;
            }
        }
    }



    public static void quickSort1(int[] array) {
        quick(array,0,array.length - 1);
    }

    //非递归实现快速排序
    public static void quickSort(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length-1;

        if(end - start +1 <= 20) {
            insertSort2(array,start,end);
            return;
        }

        int mid = threeMidNum(array,start,end);
        swap(array,mid,start);

        int pivot = parttion(array,start,end);
        if(pivot > start + 1) {
            stack.push(start);
            stack.push(pivot-1);
        }

        if(pivot < end - 1) {
            stack.push(pivot + 1);
            stack.push(end);
        }

        while(!stack.empty()) {
            end = stack.pop();
            start = stack.pop();

            if(end - start +1 <= 20) {
                insertSort2(array,start,end);
                //return;
            }else {
                mid = threeMidNum(array,start,end);
                swap(array,mid,start);

                pivot = parttion(array,start,end);
                if(pivot > start+1) {
                    stack.push(start);
                    stack.push(pivot-1);
                }

                if(pivot < end-1) {
                    stack.push(pivot + 1);
                    stack.push(end);
                }
            }
        }
    }

    /**
     *最好时间复杂度：O(N*logN)
     * 最坏时间复杂度：O(N^2) [数据是有序的或是逆序的]
     * 空间复杂度：O(logN)[最好情况]
     * 空间复杂度最坏情况：O(N)
     * 稳定性：不稳定
     * @param array
     * @param left
     * @param right
     * @return
     */
    //hoare法
    private static int parttion1(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    //挖坑法
    private static int parttion2(int[] array, int left, int right) {
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[right] = tmp;
        return left;
    }


    //前后指针法
    private static int parttion(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

    private static int threeMidNum(int[] array, int left,int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]) {
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    private static void insertSort2(int[] array,int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i -1;
            for(; j >= left; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static void quick(int[] array,int start, int end) {
        if(start >= end) {
            return;
        }

        //减少后阶层的递归
        if(end - start+1 <= 20) {
            //直接插入排序
            insertSort2(array,start,end);
            return;
        }

        //三数取中
        int mid = threeMidNum(array,start,end);
        //交换
        swap(array,mid,start);

        int pivot = parttion(array,start,end);
        quick(array,start,pivot - 1);//左树
        quick(array,pivot + 1,end);//右树
    }


    /**
     * 时间复杂度：O(NlogN)
     * 空间复杂度：O(N)
     * 稳定性：稳定  冒泡  插入  归并
     * @param array
     * @param left
     * @param mid
     * @param right
     */
    private static void merge(int[] array,int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        int[] tmpArr = new int[right-left+1];
        int k = 0; // tmpArr数组的下标

        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while(s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }

        while(s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }

        //考回原数组
        for(int i = 0; i < k; i++) {
            array[i+left] = tmpArr[i];
        }
    }

    private static void mergeSortFunc(int[] array,int left, int right) {
        if(left>=right) {
            return;
        }

        int mid = (left + right)/2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);

        merge(array,left,mid,right);
    }
    public static void mergeSort1(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }


    //归并的非递归
    public static void mergeSort(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for(int i = 0; i < array.length; i += gap*2) {
                int left = i;
                int mid = left + gap-1;
                int right = mid + gap;
                //mid 和 right 有可能越界 ，修正一下
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }

            gap *= 2;
        }
    }

    /**
     * 计数排序（非比较排序）
     * 时间复杂度：O(range+N) 也可以取 range和 N 的最大值 范围越小，时间复杂度越少
     *空间复杂度：O(range)
     */

    public static void countArray(int[] array) {
        //1.找到数组的最大值与最小值O(N)
        int maxVal = array[0];
        int minVal = array[0];
        for(int i = 1; i < array.length; i++) {
            if(array[i] < minVal) {
                minVal = array[i];
            }
            if(array[i] > maxVal) {
                maxVal = array[i];
            }
        }

        //2.可以确定数组的大小 O(N)
        int range = maxVal - minVal + 1;
        int[] count = new int[range];
        //3.再次遍历原来的数组，把原来的数据和下标进行对应，来计数
        for(int i = 0; i < array.length; i++) {
            int val = array[i];
            //开始计数，假如val的值是92,92-91则下标为1的下标++
            count[val-minVal]++;
        }
        //4.此时已经把数组中的所有数计数完成，遍历计数数组写count个i到原数组
        // 时间：O(range+n) count每个下标都会被遍历到，原本的数据个数也会被遍历
        int index = 0;
        for(int i = 0; i < count.length; i++) {
            int val = count[i];
            while(val != 0) {
                array[index] = i+minVal;
                val--;
                index++;
            }
        }

    }
    public static void main(String[] args) {
        //int[] array = {1,5,3,9,8,4,5,3,5,6,453,332,1212,33545,643,34,45,34,3,256,554};
        int[] array = {91,93,99,96,98,95,93,93,91,96};
        //insertSort(array);
        //shellSort(array);
        //selectSort1(array);
        //heapSort(array);
        //bubbleSort(array);
        //quickSort(array);
        //mergeSort(array);
        countArray(array);
        System.out.println(Arrays.toString(array));
    }
}
